﻿//=====================================================
// Copyright © 2017-2018 xuxuzhaozhao
// All rights reserved
// GUID: ee000b8a-3904-42a9-91f7-84507d16b9e9
// CLR版本: 4.0.30319.42000
// 命名空间名称: xuxuzhaozhao.Blog.Application.XuAritcleService
// 文件名: XuAritcleService
// 当前系统时间: 2017/11/8 11:31:44
// 当前登录用户名: xuxuzhaozhao
//======================================================

using System;
using System.Data.Entity;
using System.Linq;
using System.Threading.Tasks;
using xuxuzhaozhao.Blog.Application.XuAritcleService.Dto.Article;
using xuxuzhaozhao.Blog.Domain;
using xuxuzhaozhao.Blog.Infrastructure.XuEntityFramework;
using xuxuzhaozhao.Blog.Infrastructure.XuTools;

namespace xuxuzhaozhao.Blog.Application.XuAritcleService
{
    public interface IXuAritcleService
    {
        //保存文章
        XuResult Save(SaveInput input, string user);

        //删除文章
        Task<XuResult> Del(int id);

        //文章列表（前台）
        XuResult GetListFront(PageInput input);

        //文章列表（后台）
        XuResult GetListBehind(PageInput input);
    }
    public class XuAritcleService : IXuAritcleService
    {
        public async Task<XuResult> Del(int id)
        {
            var result = new XuResult();
            using (var db = new XuxuzhaozhaoDbContext())
            {
                var model = await db.XuArticle.SingleOrDefaultAsync(t => t.Id == id);
                if (model == null) return result.Fail();
                db.XuArticle.Remove(model);
                if (db.SaveChanges() > 0) return result.Success();
            }
            return result.Fail();
        }

        public XuResult GetListBehind(PageInput input)
        {
            int totalItems;
            var list = XuTools.LoadPageItems<XuArticle>
                (input.Size, input.Index, out totalItems, input.WhereLambda, input.SortLambda);

            return new XuResult()
            {
                Code = StatusCode.Success,
                Data = new
                {
                    pageData = list.ToList(),
                    totalItem = totalItems
                }
            };
        }

        public XuResult GetListFront(PageInput input)
        {
            int totalItems;
            var list = XuTools.LoadPageItems<XuArticle>
                (input.Size, input.Index, out totalItems, input.WhereLambda, input.SortLambda);

            return new XuResult()
            {
                Code = StatusCode.Success,
                Data = new
                {
                    pageData = list.ToList(),
                    totalItem = totalItems
                }
            };
        }

        public XuResult Save(SaveInput input, string user)
        {
            return input.Id > 0 ? Modified(input, user) : Add(input, user);
        }

        private XuResult Add(SaveInput input, string user)
        {
            var result = new XuResult();
            using (var db = new XuxuzhaozhaoDbContext())
            {
                var model = new XuArticle();
                model.ArticleType = input.ArticleType;
                model.Content = input.Content;
                model.Title = input.Title;
                model.Creator = user;
                db.XuArticle.Add(model);
                if (db.SaveChanges() > 0) return result.Success();
            }
            return result.Fail();
        }

        private XuResult Modified(SaveInput input, string user)
        {
            var result = new XuResult();
            using (var db = new XuxuzhaozhaoDbContext())
            {
                var model = db.XuArticle.SingleOrDefault(t => t.Id == input.Id);
                if (model == null) return result.Fail();
                model.ArticleType = input.ArticleType;
                model.Content = input.Content;
                model.Title = input.Title;
                model.ModifiedTime = DateTime.Now;
                db.Entry(model).State = EntityState.Modified;
                if (db.SaveChanges() > 0) return result.Success();
            }
            return result.Fail();
        }

        public XuResult GetModel(int id)
        {
            var result=new XuResult();
            using (var db = new XuxuzhaozhaoDbContext())
            {
                var model = db.XuArticle.SingleOrDefault(t => t.Id == id);
                //if(model==null) throw new XuException();
            }
            return result;
        }
    }
}
